# Maintainer: Philippe Renon <philippe.renon@yahoo.fr>

_realname=vrpn
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=7.34
_pkgver=0${pkgver}
pkgrel=9
pkgdesc="The Virtual-Reality Peripheral Network (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32')
url="https://github.com/vrpn/vrpn/wiki"
license=('spdx:BSL-1.0')
depends=("${MINGW_PACKAGE_PREFIX}-hidapi"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libusb"
         "${MINGW_PACKAGE_PREFIX}-python"
         "${MINGW_PACKAGE_PREFIX}-freeglut"
         "${MINGW_PACKAGE_PREFIX}-swig")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
options=('staticlibs' 'strip')
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/${_realname}/${_realname}/archive/v${_pkgver}.tar.gz"
        "0002-fix-shared-build.patch"
        "0003-link-to-glut-libraries.patch")
sha256sums=('45024722110afe4b0663b12b68ea46864e79197ea752bba90c599f87e4d51dbe'
            '21244cdc98bd23b6faf5ada5219b848bc79efe30d019af5a63e37bcb203ff687'
            'ccccbe61f031594107584e1f03f8e8e7208c9ca667579e3b582140a51d980c28')

prepare() {
  cd "${srcdir}/${_realname}-${_pkgver}"

  patch -p1 -i ${srcdir}/0002-fix-shared-build.patch
  patch -p1 -i ${srcdir}/0003-link-to-glut-libraries.patch
}

build() {
  [[ -d ${srcdir}/build-${MSYSTEM} ]] && rm -rf ${srcdir}/build-${MSYSTEM}
  mkdir ${srcdir}/build-${MSYSTEM} && cd ${srcdir}/build-${MSYSTEM}

  declare -a extra_config
  if check_option "debug" "n"; then
    extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  _pyver=$(${MINGW_PREFIX}/bin/python -c "import sys;sys.stdout.write('.'.join(map(str, sys.version_info[:2])))")

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake -Wno-dev \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DDIRECTX_ROOT_DIR=${MINGW_PREFIX} \
    -DWINHID_ROOT_DIR=${MINGW_PREFIX} \
    -DVRPN_USE_SHARED_LIBRARY=ON \
    -DVRPN_USE_WINSOCK2=ON \
    -DVRPN_BUILD_JAVA=OFF \
    -DVRPN_USE_MPI=OFF \
    -DPYTHON_LIBRARY=${MINGW_PREFIX}/bin/libpython${_pyver}.dll \
    -DPYTHON_INCLUDE_DIR=${MINGW_PREFIX}/include/python${_pyver} \
    "${extra_config[@]}" \
    ../${_realname}-${_pkgver}

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}/build-${MSYSTEM}"
  DESTDIR=${pkgdir} ${MINGW_PREFIX}/bin/cmake --install .

  install -Dm644 ${srcdir}/${_realname}-${_pkgver}/README.Legal "${pkgdir}${MINGW_PREFIX}"/share/licenses/${_realname}/LICENSE
}
